/**
 * Created by fly on 17/6/29.
 */
//闭包限定命名空间

(function ($) {

    $.fn.flyBarrage = function (options) {
        return new FlyBarrage(this,options);
    };

})(window.jQuery);


function FlyBarrage(_this,options) {
    this.options = options;
    this._this = _this;
    this._init();
};

FlyBarrage.prototype = {
    _super_number:0,
    _opts:{
        top: '0',
        left: '0',
        right: '0',
        bottom: '0',
        width: '100%',
        height: '50%',
        background:'rgba(0,0,0,0.5)',
        data:['111','33333'],//数据格式['a','b']
        paddingNumber: 30,//弹幕上下边距距离
        sleep:20,//弹幕速度系数 越大越慢
        pattern:'random',//弹幕模式 random 随机 sequence 队列模式
        sequenceSpaceTime:4000,// sequence 队列模式 每个弹幕之前间隙时间 决定了两个弹幕距离不重复
        tops:[{'top':1}, {'top':36}, {'top':71}, {'top':106}],//sequence 队列模式  设置 每列的top
        loopNumber:false//是否循环弹幕,默认关闭,设置数组为循环弹幕数量

    },
    _init:function(){

        var $this = this;

        $this._defaluts();

        $this._super_number = 0;

        // 输入出html
        $this._this.append($this._flyBoxHtml());

        $this._flyEchoBarrage($this._opts.data);

        if($this._opts.pattern == 'sequence'){

            var width = $('.fly-barrage-box').width();

            var height = $('.fly-barrage-box').height();

            var right = parseInt(Math.random()*10,10)+parseInt(Math.random()*50,10);

            var top_length = $this._opts.tops.length;

            setInterval(function () {
                for(var c=0;c<top_length;c++){

                    var dom = $('.fly_box_'+c+':last');

                    dom.removeClass('fly_box_'+c);

                    dom.animate({left:-100},{
                        'duration':(1000*$this._opts.sleep),
                        'easing':'linear',
                        'queue':'fx',
                        'complete':function () {

                            var boxDom = $(this);

                            if($this._opts.loopNumber){

                                if (boxDom.index()+1 > $this._opts.loopNumber) {

                                    boxDom.remove();

                                } else {


                                    boxDom.remove();

                                    $('.fly-barrage-box').prepend($this._loopMsg(boxDom));

                                }

                            }else {

                                boxDom.remove();
                            }

                        }

                    });


                }
            },$this._opts.sequenceSpaceTime);

        }

    },
    _defaluts:function () {

        var opts = $.extend(this._opts,this.options || {});

        this.opts = opts;

        return opts;
    },
    _flyBoxHtml:function () {
        var html = '';

        html+="<div class='fly-barrage-box' style='position: fixed;z-index: 9999;width: "+this.opts.width+";height: "+this.opts.height+";background: "+this.opts.background+";top: "+this.opts.top+";bottom: "+this.opts.bottom+";right: "+this.opts.right+";left: "+this.opts.left+";'>" +

            "</div>";

        return html;
    },
    _flyEchoBarrage:function (data) {

        var $this = this;

        var length = data.length;

        for(var i=0;i<length;i++){

            var html = "<div class='fly-barrage' data-top=''></div>";

            var dom = $(html);

            dom.html(data[i]);

            dom = $this._loopMsg(dom);

            $('.fly-barrage-box').prepend(dom);


        }

    },
    _loopMsg:function (dom) {

        var $this = this;

        var width = $('.fly-barrage-box').width();

        var height = $('.fly-barrage-box').height();


        if($this._opts.pattern == 'random'){

            var top = parseInt(Math.random()*10,10)+parseInt(Math.random()*height,10);

            if(top > (height-$this._opts.paddingNumber)){
                top = height-$this._opts.paddingNumber-parseInt(Math.random()*50,10);
            }
            if(top < $this._opts.paddingNumber){
                top = $this._opts.paddingNumber+parseInt(Math.random()*50,10);
            }

            var right = parseInt(Math.random()*10,10)+parseInt(Math.random()*width,10);

            var num = parseInt(Math.random()*6,10);

            dom.css({
                'position':'absolute',
                'white-space': 'nowrap',
                'z-index':'99999',
                'top':top+'px',
                'right':"-"+right+'px'
            });

            dom.animate({right:width+right},{
                'duration':(1000*$this._opts.sleep+right*num),
                'easing':'linear',
                'queue':'fx',
                'complete':function () {
                    var boxDom = $(this);
                    if($this._opts.loopNumber){

                        if (boxDom.index()+1 > $this._opts.loopNumber) {
                            boxDom.remove();
                        } else {
                            $this._loopMsg(boxDom);
                        }

                    }else {
                        boxDom.remove();
                    }

                },
                'step':function (rights) {

                    var boxDom = $(this);

                    if(!$this._opts.loopNumber){

                        if(rights > width+10){

                            boxDom.remove();
                        }

                    }

                }

            });


        }else {

            var top_length = $this._opts.tops.length;

            var top = $this._opts.tops[$this._super_number].top;

            var w = parseInt(Math.random()*10,10)+parseInt(Math.random()*width,10);

            width = width + w;

            dom.addClass('fly_box_'+$this._super_number);

            dom.data('top',$this._super_number);

            dom.css({
                'position':'absolute',
                'white-space': 'nowrap',
                'z-index':'99999',
                'top':top+'px',
                'left':width+'px'
            });

            $this._super_number++;

            if($this._super_number >= top_length){
                $this._super_number = 0;
            }

        }

        return dom;
    }
};